//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ExtendedBlackScholesMertonProcess.h"
using namespace Cephei::QL::Experimental::Processes;
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVolTermStructure.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/LocalVolTermStructure.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Processes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::CExtendedBlackScholesMertonProcess (boost::shared_ptr<QuantLib::ExtendedBlackScholesMertonProcess>& childNative, Object^ owner) : CGeneralizedBlackScholesProcess(CExtendedBlackScholesMertonProcess::typeid)
{
#ifdef HANDLE
	_phExtendedBlackScholesMertonProcess = NULL;
#endif
	_ppExtendedBlackScholesMertonProcess = &childNative;
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppExtendedBlackScholesMertonProcess));
}
Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::CExtendedBlackScholesMertonProcess (QuantLib::ExtendedBlackScholesMertonProcess& childNative, Object^ owner) : CGeneralizedBlackScholesProcess(CExtendedBlackScholesMertonProcess::typeid)
{
#ifdef HANDLE
	_phExtendedBlackScholesMertonProcess = NULL;
#endif
	_ppExtendedBlackScholesMertonProcess = new boost::shared_ptr<QuantLib::ExtendedBlackScholesMertonProcess> (&childNative);
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppExtendedBlackScholesMertonProcess));
    _ExtendedBlackScholesMertonProcessOwner = owner;
    _GeneralizedBlackScholesProcessOwner = owner;
}

Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::CExtendedBlackScholesMertonProcess (CExtendedBlackScholesMertonProcess^ copy) : CGeneralizedBlackScholesProcess(CExtendedBlackScholesMertonProcess::typeid)
{
#ifdef HANDLE
	_phExtendedBlackScholesMertonProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppExtendedBlackScholesMertonProcess = new boost::shared_ptr<QuantLib::ExtendedBlackScholesMertonProcess> (copy->GetShared());
        _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppExtendedBlackScholesMertonProcess));
    }
}
Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::CExtendedBlackScholesMertonProcess (System::Type^ t) : CGeneralizedBlackScholesProcess(CExtendedBlackScholesMertonProcess::typeid)
{
#ifdef HANDLE
	_phExtendedBlackScholesMertonProcess = NULL;
#endif
	if (!t->IsSubclassOf(CExtendedBlackScholesMertonProcess::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::CExtendedBlackScholesMertonProcess (QuantLib::Handle<QuantLib::ExtendedBlackScholesMertonProcess>& childNative, Object^ owner)  : CGeneralizedBlackScholesProcess(CExtendedBlackScholesMertonProcess::typeid)
{
	_phExtendedBlackScholesMertonProcess = &childNative;
	_ppExtendedBlackScholesMertonProcess = &static_cast<boost::shared_ptr<QuantLib::ExtendedBlackScholesMertonProcess>>(childNative.currentLink());
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppExtendedBlackScholesMertonProcess));
    _ExtendedBlackScholesMertonProcessOwner = owner;
}
Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::CExtendedBlackScholesMertonProcess (QuantLib::Handle<QuantLib::ExtendedBlackScholesMertonProcess> childNative)  : CGeneralizedBlackScholesProcess(CExtendedBlackScholesMertonProcess::typeid)
{
	_phExtendedBlackScholesMertonProcess = &childNative;
	_ppExtendedBlackScholesMertonProcess = &static_cast<boost::shared_ptr<QuantLib::ExtendedBlackScholesMertonProcess>>(childNative.currentLink());
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppExtendedBlackScholesMertonProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::CExtendedBlackScholesMertonProcess (QuantLib::ExtendedBlackScholesMertonProcess childNative)  : CGeneralizedBlackScholesProcess(CExtendedBlackScholesMertonProcess::typeid)
{
#ifdef HANDLE
	_phExtendedBlackScholesMertonProcess = NULL;
#endif
	_ppExtendedBlackScholesMertonProcess = new boost::shared_ptr<QuantLib::ExtendedBlackScholesMertonProcess> (new QuantLib::ExtendedBlackScholesMertonProcess (childNative));
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppExtendedBlackScholesMertonProcess));
}
#endif

Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::~CExtendedBlackScholesMertonProcess ()
{
    if (_ppExtendedBlackScholesMertonProcess != NULL)
    {
	    delete _ppExtendedBlackScholesMertonProcess;
        _ppExtendedBlackScholesMertonProcess = NULL;
    }
}
Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::!CExtendedBlackScholesMertonProcess ()
{
    if (_ppExtendedBlackScholesMertonProcess != NULL)
    {
	    delete _ppExtendedBlackScholesMertonProcess;
    }
}
QuantLib::ExtendedBlackScholesMertonProcess& Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::GetReference ()
{
    if (_ppExtendedBlackScholesMertonProcess == NULL) throw gcnew NativeNullException ();
	return **_ppExtendedBlackScholesMertonProcess;
}
boost::shared_ptr<QuantLib::ExtendedBlackScholesMertonProcess>& Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::GetShared ()
{
    if (_ppExtendedBlackScholesMertonProcess == NULL) throw gcnew NativeNullException ();
	return *_ppExtendedBlackScholesMertonProcess;
}
QuantLib::ExtendedBlackScholesMertonProcess* Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::GetPointer ()
{
    if (_ppExtendedBlackScholesMertonProcess == NULL) throw gcnew NativeNullException ();
	return &**_ppExtendedBlackScholesMertonProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ExtendedBlackScholesMertonProcess>& Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::GetHandle ()
{
	if (_phExtendedBlackScholesMertonProcess == NULL)
	{
		_phExtendedBlackScholesMertonProcess = new Handle<QuantLib::ExtendedBlackScholesMertonProcess> (*_ppExtendedBlackScholesMertonProcess);
	}
	return *_phExtendedBlackScholesMertonProcess;
}
#endif
bool Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::HasNative () 
{
	return (_ppExtendedBlackScholesMertonProcess != NULL);
}

Double Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::Diffusion (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppExtendedBlackScholesMertonProcess)->diffusion ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::Drift (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppExtendedBlackScholesMertonProcess)->drift ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Processes::CExtendedBlackScholesMertonProcess::Evolve (Double t0, Double x0, Double dt, Double dw)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0);
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0);
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt);
        QuantLib::Real _dw = (QuantLib::Real)ValueHelper::Convert (dw);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppExtendedBlackScholesMertonProcess)->evolve ( _t0,  _x0,  _dt,  _dw );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

